package com.vxb.testmvp.test_zonghe.test_2;

import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.holenzhou.pullrecyclerview.PullRecyclerView;
import com.holenzhou.pullrecyclerview.layoutmanager.XGridLayoutManager;
import com.vxb.testmvp.R;
import com.vxb.testmvp.test_zonghe.entity.ZhuangEntity;
import com.vxb.testmvp.test_zonghe.network.NetworkManager;
import com.vxb.testmvp.util.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class TestTowActivity extends AppCompatActivity {

    @Bind(R.id.pull_recyclerview)
    PullRecyclerView mPullRecyclerView;
    private CompositeSubscription mCompositeSubscription;
    private TestTowAdapter mAdapter;

    private List<ZhuangEntity> mDataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_tow);

        ButterKnife.bind(this);

        mCompositeSubscription = new CompositeSubscription();

        initView();
        // initData();
    }

    private void initData() {

        mCompositeSubscription.add(NetworkManager.getZhuangbiApi()
                .search("装逼")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Action1<List<ZhuangEntity>>() {
                            @Override
                            public void call(List<ZhuangEntity> zhuangEntities) {

                                mDataList.clear();

                                mDataList.addAll(zhuangEntities);
                                mAdapter.notifyDataSetChanged();
                                mPullRecyclerView.stopRefresh();


                            }
                        },
                        new Action1<Throwable>() {

                            @Override
                            public void call(Throwable throwable) {
                                throwable.printStackTrace();
                                mPullRecyclerView.enablePullRefresh(false);
                                mPullRecyclerView.stopRefresh();
                                Util.showToast(getApplication(), "加载错误,网络出问题了.");
                                //  Util.makeSnck(mPullRecyclerView,"网络出问题了");
                                Snackbar.make(mPullRecyclerView, "网络出问题了", Snackbar.LENGTH_INDEFINITE)
                                        .setAction("重试", new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                mPullRecyclerView.enablePullRefresh(false);
                                                mPullRecyclerView.postRefreshing();
                                            }
                                        })
                                        .show();


                            }
                        }));


    }

    int pageSize = 0;

    private void initView() {

        // 初始化PullRecyclerView
        mPullRecyclerView.setLayoutManager(new XGridLayoutManager(TestTowActivity.this, 2));

        mAdapter = new TestTowAdapter(this, R.layout.grid_item, mDataList);
        mPullRecyclerView.setAdapter(mAdapter);


        mPullRecyclerView.setColorSchemeResources(R.color.colorAccent); // 设置下拉刷新的旋转圆圈的颜色

        mPullRecyclerView.enablePullRefresh(true); // 开启下拉刷新，默认即为true，可不用设置
        mPullRecyclerView.enableLoadDoneTip(true, R.string.load_done_tip); // 开启数据全部加载完成时的底部提示，默认为false
        mPullRecyclerView.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {
            @Override
            public void onPullRefresh() {

                initData();
                //             mPullRecyclerView.enableLoadMore(true); // 当剩余还有大于0页的数据时，开启上拉加载更多
                //           pageSize = 2;
            }

            @Override
            public void onLoadMore() {

                //  initData();
                //pageSize--;
                // mPullRecyclerView.stopLoadMore();
            }
        });
        mPullRecyclerView.postRefreshing();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

}
